package com.zues.thread.method;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 测试线程的礼让
 */
@Slf4j(topic = "c.CreateDemo")
public class MethodDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //方式一
        new Thread("t1"){
            @Override
            public void run() {
                for (int i = 0; i < 250; i++) {
                    System.out.println(Thread.currentThread().getName()+" "+i);
                    if(i == 50){
                        //线程礼让，让出当前线程，执行其他线程
                        Thread.yield();
                    }
                }
                log.debug("方式一");
            }
        }.start();
        //方式二
        new Thread(()->{
            for (int i = 0; i < 50; i++) {
                System.out.println(Thread.currentThread().getName()+" "+i);
            }
            log.debug("方式二");
        },"t2").start();

        //方式三
//        FutureTask<Integer> futureTask = new FutureTask<Integer>(()->{
//            for (int i = 0; i < 50; i++) {
//                System.out.println(Thread.currentThread().getName()+" "+i);
//            }
//            log.debug("方式三");
//            return 1;
//        });
//        new Thread(futureTask,"t3").start();
//        log.debug("方式三的返回值 = {}",futureTask.get());
    }

}
